﻿using SIG.DAL.Dapper.Model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIG.DAL.Dapper.Repositories
{
    public class EmailAccountRepository
    {
        private DbConnection _connection;

        public EmailAccount GetById(int id)
        {
            EmailAccount result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Get<EmailAccount>(id);
            }
            return result;
        }

        public EmailAccount GetDefaultAccount()
        {
            EmailAccount result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<EmailAccount>("where IsDefault=1").FirstOrDefault();
            }
            return result;
        }
        public int? Create(EmailAccount model)
        {
            int? result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Insert(model);
            }
            return result;
        }

        public int Update(EmailAccount model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Update(model);
            }
            return result;
        }
        public int Delete(EmailAccount model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete(model);
            }
            return result;
        }

        public int Delete(int id)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete<EmailAccount>(id);
            }
            return result;
        }
        public IEnumerable<EmailAccount> GetItems()
        {
            IEnumerable<EmailAccount> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<EmailAccount>();
            }
            return result;
        }

        public EmailAccount SetDefault(int id)
        {
            EmailAccount result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Get<EmailAccount>(id);
                if (result == null)
                {
                    return null;
                }

                if (!result.IsDefault)
                {
                    var accounts = _connection.GetList<EmailAccount>("where IsDefault=1 and ID<>@Id", new { Id = id });
                    foreach (var item in accounts)
                    {
                        item.IsDefault = false;
                        Update(item);
                    }
                    result.IsDefault = true;
                }
                else
                {
                    result.IsDefault = false;
                }
                Update(result);
            }
            return result;

        }



      
        public IEnumerable<EmailAccount> GetListPagedItems(int pageIndex, int pageSize, out int totalCount)
        {
            IEnumerable<EmailAccount> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                totalCount = _connection.RecordCount<EmailAccount>();
                result = _connection.GetListPaged<EmailAccount>(pageIndex, pageSize, null, "CreatedDate desc");
            }
            return result;
        }

        public IEnumerable<EmailAccount> GetListPagedItems(int pageIndex, int pageSize, string keyword, out int totalCount)
        {
            IEnumerable<EmailAccount> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                keyword = !string.IsNullOrEmpty(keyword) ? keyword.Replace("[", "[[]").Replace("%", "[%]") : keyword;
                string likeKeyword = "%" + keyword + "%";

                totalCount = _connection.RecordCount<EmailAccount>("where Email like @Keyword or UserName like @Keyword",
                    new { Keyword = likeKeyword });
                result = _connection.GetListPaged<EmailAccount>(pageIndex, pageSize, "where Email like @Keyword or UserName like @Keyword",
                    "CreatedDate desc",
                    new { Keyword = likeKeyword });
            }
            return result;
        }
    }

}
